package com.gateway.common.dto.convert.rule;

import com.gateway.common.constant.Constants;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/24 10:09
 **/
public class Resilience4JHandle {

    /**
     * ratelimiter timeoutDurationRate.
     */
    private int timeoutDurationRate = Constants.TIMEOUT_DURATION_RATE;

    /**
     * ratelimiter limitRefreshPeriod.
     */
    private int limitRefreshPeriod = Constants.LIMIT_REFRESH_PERIOD;

    /**
     * ratelimiter limitForPeriod.
     */
    private int limitForPeriod = Constants.LIMIT_FOR_PERIOD;

    /**
     * circuitBreaker circuitEnable.
     */
    private int circuitEnable = Constants.CIRCUIT_DISABLE;


    /**
     * circuitBreaker circuitEnable.
     */
    private int rateLimitEnable = Constants.CIRCUIT_DISABLE;

    /**
     * circuitBreaker timeoutDuration.
     */
    private long timeoutDuration = Constants.TIMEOUT_DURATION;

    /**
     * circuitBreaker timeoutDuration.
     */
    private String fallbackUri;

    /**
     * circuitBreaker slidingWindowSize.
     */
    private int slidingWindowSize = Constants.SLIDING_WINDOW_SIZE;

    /**
     * circuitBreaker slidingWindowType.
     */
    private int slidingWindowType = Constants.SLIDING_WINDOW_TYPE;

    /**
     * circuitBreaker minimumNumberOfCalls.
     */
    private int minimumNumberOfCalls = Constants.MINIMUM_NUMBER_OF_CALLS;

    /**
     * circuitBreaker waitIntervalFunctionInOpenState.
     */
    private int waitIntervalFunctionInOpenState = Constants.WAIT_INTERVAL_FUNCTION_IN_OPEN_STATE;

    /**
     * circuitBreaker waitIntervalFunctionInOpenState.
     */
    private int permittedNumberOfCallsInHalfOpenState = Constants.PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE;

    /**
     * circuitBreaker failureRateThreshold.
     */
    private float failureRateThreshold = Constants.FAILURE_RATE_THRESHOLD;

    /**
     * circuitBreaker automaticTransitionFromOpenToHalfOpenEnabled.
     */
    private Boolean automaticTransitionFromOpenToHalfOpenEnabled = Constants.AUTOMATIC_TRANSITION_FROM_OPEN_TO_HALF_OPEN_ENABLED;


    public int getTimeoutDurationRate() {
        return timeoutDurationRate;
    }

    public void setTimeoutDurationRate(int timeoutDurationRate) {
        this.timeoutDurationRate = timeoutDurationRate;
    }

    public int getLimitRefreshPeriod() {
        return limitRefreshPeriod;
    }

    public void setLimitRefreshPeriod(int limitRefreshPeriod) {
        this.limitRefreshPeriod = limitRefreshPeriod;
    }

    public int getLimitForPeriod() {
        return limitForPeriod;
    }

    public void setLimitForPeriod(int limitForPeriod) {
        this.limitForPeriod = limitForPeriod;
    }

    public int getCircuitEnable() {
        return circuitEnable;
    }

    public void setCircuitEnable(int circuitEnable) {
        this.circuitEnable = circuitEnable;
    }

    public long getTimeoutDuration() {
        return timeoutDuration;
    }

    public void setTimeoutDuration(long timeoutDuration) {
        this.timeoutDuration = timeoutDuration;
    }

    public String getFallbackUri() {
        return fallbackUri;
    }

    public void setFallbackUri(String fallbackUri) {
        this.fallbackUri = fallbackUri;
    }

    public int getSlidingWindowSize() {
        return slidingWindowSize;
    }

    public void setSlidingWindowSize(int slidingWindowSize) {
        this.slidingWindowSize = slidingWindowSize;
    }

    public int getSlidingWindowType() {
        return slidingWindowType;
    }

    public void setSlidingWindowType(int slidingWindowType) {
        this.slidingWindowType = slidingWindowType;
    }

    public int getMinimumNumberOfCalls() {
        return minimumNumberOfCalls;
    }

    public void setMinimumNumberOfCalls(int minimumNumberOfCalls) {
        this.minimumNumberOfCalls = minimumNumberOfCalls;
    }

    public int getWaitIntervalFunctionInOpenState() {
        return waitIntervalFunctionInOpenState;
    }

    public void setWaitIntervalFunctionInOpenState(int waitIntervalFunctionInOpenState) {
        this.waitIntervalFunctionInOpenState = waitIntervalFunctionInOpenState;
    }

    public int getPermittedNumberOfCallsInHalfOpenState() {
        return permittedNumberOfCallsInHalfOpenState;
    }

    public void setPermittedNumberOfCallsInHalfOpenState(int permittedNumberOfCallsInHalfOpenState) {
        this.permittedNumberOfCallsInHalfOpenState = permittedNumberOfCallsInHalfOpenState;
    }

    public float getFailureRateThreshold() {
        return failureRateThreshold;
    }

    public void setFailureRateThreshold(float failureRateThreshold) {
        this.failureRateThreshold = failureRateThreshold;
    }

    public Boolean getAutomaticTransitionFromOpenToHalfOpenEnabled() {
        return automaticTransitionFromOpenToHalfOpenEnabled;
    }

    public void setAutomaticTransitionFromOpenToHalfOpenEnabled(Boolean automaticTransitionFromOpenToHalfOpenEnabled) {
        this.automaticTransitionFromOpenToHalfOpenEnabled = automaticTransitionFromOpenToHalfOpenEnabled;
    }

    public int getRateLimitEnable() {
        return rateLimitEnable;
    }

    public void setRateLimitEnable(int rateLimitEnable) {
        this.rateLimitEnable = rateLimitEnable;
    }

    /**
     * check filed default value.
     *
     * @param resilience4JHandle {@linkplain Resilience4JHandle}
     */
    public void checkData(final Resilience4JHandle resilience4JHandle) {
        resilience4JHandle.setTimeoutDurationRate(resilience4JHandle.getTimeoutDurationRate() < 0 ? Constants.TIMEOUT_DURATION_RATE : resilience4JHandle.getTimeoutDurationRate());
        resilience4JHandle.setLimitRefreshPeriod(resilience4JHandle.getLimitRefreshPeriod() < 0 ? Constants.LIMIT_REFRESH_PERIOD : resilience4JHandle.getLimitRefreshPeriod());
        resilience4JHandle.setLimitForPeriod(resilience4JHandle.getLimitForPeriod() < 0 ? Constants.LIMIT_FOR_PERIOD : resilience4JHandle.getLimitForPeriod());
        resilience4JHandle.setCircuitEnable(resilience4JHandle.getCircuitEnable() != Constants.CIRCUIT_ENABLE ? Constants.CIRCUIT_DISABLE : Constants.CIRCUIT_ENABLE);
        resilience4JHandle.setTimeoutDuration(resilience4JHandle.getTimeoutDuration() < 0 ? Constants.TIMEOUT_DURATION : resilience4JHandle.getTimeoutDuration());
        resilience4JHandle.setFallbackUri(!"0".equals(resilience4JHandle.getFallbackUri()) ? resilience4JHandle.getFallbackUri() : "");
        resilience4JHandle.setSlidingWindowSize(resilience4JHandle.getSlidingWindowSize() < 0 ? Constants.SLIDING_WINDOW_SIZE : resilience4JHandle.getSlidingWindowSize());
        resilience4JHandle.setSlidingWindowType(resilience4JHandle.getSlidingWindowType() < 0 ? Constants.SLIDING_WINDOW_TYPE : resilience4JHandle.getSlidingWindowType());
        resilience4JHandle.setMinimumNumberOfCalls(resilience4JHandle.getMinimumNumberOfCalls() < 0 ? Constants.MINIMUM_NUMBER_OF_CALLS : resilience4JHandle.getMinimumNumberOfCalls());
        resilience4JHandle.setWaitIntervalFunctionInOpenState(resilience4JHandle.getWaitIntervalFunctionInOpenState() < 0
                ? Constants.WAIT_INTERVAL_FUNCTION_IN_OPEN_STATE : resilience4JHandle.getWaitIntervalFunctionInOpenState());
        resilience4JHandle.setPermittedNumberOfCallsInHalfOpenState(resilience4JHandle.getPermittedNumberOfCallsInHalfOpenState() < 0
                ? Constants.PERMITTED_NUMBER_OF_CALLS_IN_HALF_OPEN_STATE : resilience4JHandle.getPermittedNumberOfCallsInHalfOpenState());
        resilience4JHandle.setFailureRateThreshold(
                resilience4JHandle.getFailureRateThreshold() < 0 || resilience4JHandle.getFailureRateThreshold() > 100
                        ? Constants.FAILURE_RATE_THRESHOLD : resilience4JHandle.getFailureRateThreshold());
    }
}
